package com.mobjam.workingtitleforagame;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

@SuppressLint("UseValueOf")
public class Map {
	public boolean moveCharacter(int newColumn, int newRow) 
	{
		return character.getCurrentRegion().moveObject(character, newColumn, newRow );
	}

	public boolean moveCharacterNorth()
	{
		return character.getCurrentRegion().moveObjectNorth(character);
	}
	
	public boolean moveCharacterSouth()
	{
		return character.getCurrentRegion().moveObjectSouth(character);
	}
	
	public boolean moveCharacterEast()
	{
		return character.getCurrentRegion().moveObjectEast(character);
	}
	
	public boolean moveCharacterWest()
	{
		return character.getCurrentRegion().moveObjectWest(character);
	}
	
	public Character getCharacter() {
		return character;
	}
	public void setCharacter(Character character) {
		this.character = character;
	}
	public int getRegionHeight() {
		return regionHeight;
	}
	public void setRegionHeight(int regionHeight) {
		this.regionHeight = regionHeight;
	}
	public int getRegionWidth() {
		return regionWidth;
	}
	public void setRegionWidth(int regionWidth) {
		this.regionWidth = regionWidth;
	}
	public int getMapHeight() {
		return mapHeight;
	}
	public void setMapHeight(int mapHeight) {
		this.mapHeight = mapHeight;
	}
	public int getMapWidth() {
		return mapWidth;
	}
	public void setMapWidth(int mapWidth) {
		this.mapWidth = mapWidth;
	}
	public int getTileHeight() {
		return tileHeight;
	}
	public void setTileHeight(int tileHeight) {
		this.tileHeight = tileHeight;
	}

	public int getTileWidth() {
		return tileWidth;
	}
	public void setTileWidth(int tileWidth) {
		this.tileWidth = tileWidth;
	}

	private int tileHeight;
	private int tileWidth;
	
	protected Character character;
	
	private MapRegion[][] mapRegions;
	private int mapHeight;
	private int mapWidth;

	private int regionHeight;
	private int regionWidth;
	
	private int currentRegionX;
	private int currentRegionY;
	
	public MapRegion getCurrentRegion( )
	{
		if( mapRegions != null && currentRegionX < mapWidth && currentRegionY < mapHeight )
		{
			return mapRegions[currentRegionX][currentRegionY];
		}
		
		return null;
	}
	
	public Map( Context context, int mapWidth, int mapHeight,
			int regionWidth, int regionHeight, int tileWidth, int tileHeight, 
			int currentRegionX, int currentRegionY, int  characterInRegionX, int characterInRegionY )
	{
		this.mapWidth = mapWidth;
		this.mapHeight = mapHeight;
		this.regionWidth = regionWidth;
		this.regionHeight = regionHeight;
		this.tileWidth = tileWidth;
		this.tileHeight = tileHeight;
		this.currentRegionX = currentRegionX;
		this.currentRegionY = currentRegionY;
		
		character = Character.getInstance();
		character.initCharacter(MapObjectType.GIRLCHARACTER, 1000 );

		populateRegions(context,characterInRegionX,characterInRegionY);		
		mapRegions[currentRegionX][currentRegionY].generateBitmap();
	}

	public void populateRegions(Context context, int characterInRegionX, int characterInRegionY )
	{
		mapRegions = new MapRegion[ mapWidth][mapHeight];
		for( int row = 0; row < mapHeight; row++)
		{
			for(int column=0; column < mapWidth; column++)
			{
				mapRegions[column][row] = new MapRegion(context, regionWidth, regionHeight, tileWidth, tileHeight );
				
				if( row > 0 )
				{
					mapRegions[column][row].setNorthRegion(mapRegions[column][row-1]);
					mapRegions[column][row-1].setSouthRegion(mapRegions[column][row]);
				}
				
				if( column > 0 )
				{
					mapRegions[column][row].setWestRegion(mapRegions[column-1][row]);
					mapRegions[column-1][row].setEastRegion(mapRegions[column][row]);
				}
				
				if( column == currentRegionX && row == currentRegionY )
				{
					mapRegions[column][row].addChildMapObject( character, characterInRegionX, characterInRegionY, true );
				}
			}
		}

	}
	
	/**
	 * @return the mapRegions
	 */
	public MapRegion[][] getMapRegions() {
		return mapRegions;
	}
	/**
	 * @param mapRegions the mapRegions to set
	 */
	public void setMapRegions(MapRegion[][] mapRegions) {
		this.mapRegions = mapRegions;
	}
}
